package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.annotation.RequiresPermissionsDesc;
import com.cskaoyan.market.bo.AdCreatBo;
import com.cskaoyan.market.db.domain.MarketAd;
import com.cskaoyan.market.service.admin.MarketAdService;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/admin/ad")
@Validated
public class AdminAdController {

    private final Log logger = LogFactory.getLog(AdminAdController.class);
    @Autowired
    MarketAdService marketAdService;

    @RequiresPermissions("admin:ad:list")
    @RequiresPermissionsDesc(menu = {"推广管理", "广告管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String page,String limit,String sort,String order,String name,String content)  {
        // 设定默认参数
        String defaultPage = "1";
        String defaultLimit = "20";
        // 参数校验
        if (page == null || page.equals("")) {
            page = defaultPage;
        }
        if (limit == null || limit.equals("")) {
            page = defaultLimit;
        }
        // sort若为空，则取默认值add_time
        if (StringUtils.isEmpty(sort)) {
            sort = "add_time";
        }
        // order若为空，则取默认值desc - 降序
        if (StringUtils.isEmpty(order)) {
            order = "desc";
        }


        List<MarketAd> result = marketAdService.list(page, limit, sort, order, name, content);

        return ResponseUtil.okList(result);
    }

    @RequiresPermissions("admin:ad:create")
    @RequiresPermissionsDesc(menu = {"推广管理", "广告管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody AdCreatBo creatBo)  {
        // {"name":"斯卡蒂","content":"带我走吧~","url":"http://101.43.69.31:8083/wx/storage/fetch/6g3kisuoecznu53vfkv8.png","link":"youheart","position":1,"enabled":true}

        String content = creatBo.getContent();
        Boolean enabled = creatBo.getEnabled();
        String link = creatBo.getLink();
        String name = creatBo.getName();
        Byte position = creatBo.getPosition();
        String url = creatBo.getUrl();

        MarketAd marketAd = new MarketAd();

        // 参数校验
        // 1.广告标题name不能为空
        // 2.广告内容content不能为空
        // 3.广告链接url不能为空
        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(content) || StringUtils.isEmpty(url)) {
            return ResponseUtil.badArgument();
        }
        marketAd.setContent(content);
        marketAd.setEnabled(enabled);
        marketAd.setLink(link);
        marketAd.setName(name);
        marketAd.setPosition(position);
        marketAd.setUrl(url);
        marketAd.setAddTime(LocalDateTime.now());
        marketAd.setUpdateTime(LocalDateTime.now());
        marketAd.setDeleted(false);

        //根据resCode是否是1还是0判断广告管理信息是否上传成功
        int row = marketAdService.create(marketAd);
        if (row == 0) {
            // 上传失败
            return ResponseUtil.badArgument();
        }
        // 上传成功
        // 设置响应体
        return ResponseUtil.ok(marketAd);

    }

    @RequiresPermissions("admin:ad:update")
    @RequiresPermissionsDesc(menu = {"推广管理", "广告管理"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody MarketAd marketAd)  {

        String content = marketAd.getContent();
        String name = marketAd.getName();
        String url = marketAd.getUrl();

        // 参数校验
        // 1.广告标题name不能为空
        // 2.广告内容content不能为空
        // 3.广告链接url不能为空
        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(content) || StringUtils.isEmpty(url)) {
            return ResponseUtil.badArgument();
        }
        //根据resCode是否是1还是0判断广告管理信息是否更新成功
        int row = marketAdService.update(marketAd);
        if (row == 0) {
            // 更新失败
            return ResponseUtil.updatedDataFailed();
        }
        // 更新成功
        // 设置响应体
        return ResponseUtil.ok(marketAd);
    }

    @RequiresPermissions("admin:ad:delete")
    @RequiresPermissionsDesc(menu = {"推广管理", "广告管理"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody MarketAd marketAd)  {

        int resCode = marketAdService.delete(marketAd);
        if (resCode == 0) {
            return ResponseUtil.fail();
        }
        return ResponseUtil.ok(marketAd);

    }

}
